package Offer.offer;

import domain.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 从上到下打印二叉树
 * 层序遍历
 * @author leiwenkang
 * @date 2020/4/6
 */
public class Test23从上到下打印二叉树 {
    public void printTree(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();

        TreeNode p = root;
        queue.add(p);
        while (!queue.isEmpty()) {
            TreeNode node = queue.remove();
            System.out.println(node.val);
            if (node.left != null) {
                queue.add(node.left);
            }

            if (node.right != null) {
                queue.add(node.right);
            }
        }
    }
}
